<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户添加</title>
    <link type="text/css" rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" name="userName" required  lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="userPwd" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="userType" lay-verify="required">
                    <option value=""></option>
                    <option value="0">管理员</option>
                    <option value="1">用户</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <input type="hidden" name="userImg"/>
            <img id="img" style="width: 200px;height: 200px">
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script type="text/javascript" src="/layui/layui.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        layui.use('form', function(){
            var form = layui.form;
            var $ = layui.$;
            //监听提交
            form.on('submit(formDemo)', function(data){
                //使用post提交后台
                axios.post("/user",{
                    userName:$("[name=userName]").val(),
                    userPwd:$("[name=userPwd]").val(),
                    userType:$("[name=userType]").val(),
                    userImg:$("[name=userImg]").val()
                }).then(function (value) {
                    // alert(value.data);
                    if(value.data == "ok"){
                        // 调用父页面的load方法
                        parent.location.reload();
                        // 通过父页面关闭当前页面
                        var index = parent.layer.getFrameIndex(window.name);
                        //先得到当前iframe层的索引
                        parent.layer.close(index);
                    }
                });
                return false;
            });
        });
        layui.use('upload', function(){
            var upload = layui.upload;
            var $ = layui.$;
            //执行实例
            var uploadInst = upload.render({
                elem: '#test1' //绑定元素
                ,url: '/upload' //上传接口
                ,done: function(res){
                    //上传完毕回调
                    // alert(res);
                    console.log(res);
                    $("#img").attr("src","http://localhost:8888/upload/" + res.data);
                    $("[name=userImg]").val("http://localhost:8888/upload/" + res.data);
                }
                ,error: function(){
                    //请求异常回调
                }
            });
        });
    </script>
</body>
</html>